<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/6
 * Time: 16:01
 */
namespace App\Services;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use App\Models\MediaCommentConfig;

class MediaCommentConfigService
{
    private static $_msg = ['code' => 0, 'msg' => ''];

    /**
     * @desc 创建视频评论配置
     * @param $params
     * @return mixed
     */
    public static function add_media_comment_config($params)
    {
        if(!self::_check_data($params)) {
            return self::$_msg;
        }

        $data = self::_handle_data($params);
        $data['created_at'] = $data['updated_at'] = time2date();
        if(!MediaCommentConfig::insert($data)) {
            self::_set_msg('10000', '写入视频评论配置失败');
        }

        return self::$_msg;
    }

    /**
     * @desc 修改视频评论配置
     * @param $params
     * @return mixed
     */
    public static function modify_media_comment_config($params) {
        if(!self::_check_data($params)) {
            return self::$_msg;
        }

        $where = ['id' => $params['id']];
        $data = self::_handle_data($params);
        $data['updated_at'] = time2date();
        if(!MediaCommentConfig::where($where)->update($data)) {
            self::_set_msg('10000', '修改视频评论配置失败');
        }

        return self::$_msg;
    }

    /**
     * @desc 修改视频评论配置状态
     * @param $params
     * @return array
     */
    public static function modify_media_comment_config_status($params)
    {
        $is_record = MediaCommentConfig::where(['id' => $params['id']])->first();
        if(!$is_record) {
            self::_set_msg('10000', '视频评论配置数据异常');
            return self::$_msg;
        }

        if($is_record->status == 3) {
            self::_set_msg('10000', '视频评论配置已结束');
            return self::$_msg;
        }

        if(!in_array($params['status'], [1, 2, 3])) {
            self::_set_msg('10000', '视频评论配置状态异常');
            return self::$_msg;
        }

        if($params['status'] == $is_record->status) {
            return self::$_msg;
        }
        $data = ['operator_id' => $params['operator_id'], 'status' => $params['status'], 'updated_at' => time2date()];
        if(!MediaCommentConfig::where(['id' => $params['id']])->update($data)) {
            self::_set_msg('10000', '修改失败');
        }

        return self::$_msg;
    }

    /**
     * @desc 检测评论配置数据
     * @param $params
     * @return bool
     */
    private static function _check_data($params)
    {
        if(mb_strlen($params['config_name']) > 20) {
            self::_set_msg('10000', '视频评论配置名称不能超过20个字符');
            return false;
        }
        // 检测评论对象
        if(!in_array($params['media_type'], [1])) {
            self::_set_msg('10000', '视频评论对象异常');
            return false;
        }

        // 检测评论频率
        if(!in_array($params['frequency'], [1])) {
            self::_set_msg('10000', '视频评论频率异常');
            return false;
        }

        // 检测评论的周期类型(1:天;2:时;3:分;),
        if(!in_array($params['cycle_type'], [1])) {
            self::_set_msg('10000', '视频评论周期类型异常');
            return false;
        }

        // 检测周期时间
        if(!is_int($params['cycle_time'])) {
            self::_set_msg('10000', '视频评论周期时间为整型');
            return false;
        }

        if($params['cycle_time'] != 1) {
            self::_set_msg('10000', '视频评论周期时间不合法');
            return false;
        }

        if(mb_strlen($params['comment_content']) > 200) {
            self::_set_msg('10000', '视频评论内容不能超过200个字符');
            return false;
        }

        if(!$start_time = strtotime($params['start_time'])) {
            self::_set_msg('10000', '开始时间格式不正确');
            return false;
        }

        if(!$end_time = strtotime($params['end_time'])) {
            self::_set_msg('10000', '结束时间格式不正确');
            return false;
        }

        if($start_time > $end_time) {
            self::_set_msg('10000', '开始时间不能大于结束时间');
            return false;
        }

        // 检测视频配置状态
        if(!empty($params['id'])) {
            $is_record = MediaCommentConfig::where(['id' => $params['id']])->first();
            if(!$is_record) {
                self::_set_msg('10000', '视频评论配置数据异常');
                return false;
            }

            if($is_record->status == 3) {
                self::_set_msg('10000', '视频评论配置已结束');
                return false;
            }
        }

        return true;
    }

    /**
     * @desc 整合数据
     * @param $params
     * @return array
     */
    private static function _handle_data($params)
    {
        return $data = [
            'config_name' => trim($params['config_name']),
            'media_type' => $params['media_type'],
            'frequency' => $params['frequency'],
            'cycle_type' => $params['cycle_type'],
            'cycle_time' => $params['cycle_time'],
            'comment_content' => $params['comment_content'],
            'start_time' => strtotime($params['start_time']),
            'end_time' => strtotime($params['end_time']),
            'operator_id' => $params['operator_id'],
        ];
    }

    /**
     * @desc 设置提示信息
     * @param $code
     * @param $msg
     * @return array
     */
    private static function _set_msg($code, $msg)
    {
        return self::$_msg = ['code' => $code, 'msg' => $msg];
    }
}